using System;
using System.Text;
using System.Xml;
using System.Collections;


using gudusoft.gsqlparser;
using gudusoft.gsqlparser.Units;


namespace xmloutput
{
    class basicnodexml
    {
        //public static int i;
        //public static XmlWriterSettings exprxmlsettings;

        //public static StringBuilder ExprXmlText;
        //public static XmlWriter exprwriter;
        //public static string exprinternaltext;
        public static Stack exprstack = new Stack(); 

        public static string TLz_AttrXml(TLz_Attr pAttr)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            settings.Indent = true;
            settings.IndentChars = ("    ");

            StringBuilder XmlText = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(XmlText, settings))
            {
                // Write XML data.
                writer.WriteStartElement(pAttr.GetType().Name);
                writer.WriteAttributeString("text", pAttr.AsText);
                if (pAttr.ServerNameToken != null)
                {
                    writer.WriteElementString("ServerName", pAttr.ServerNameToken.AsText);
                }
                if (pAttr.DatabaseNameToken != null)
                {
                    writer.WriteElementString("DatabaseName", pAttr.DatabaseNameToken.AsText);
                }
                if (pAttr.SchemaNameToken != null)
                {
                    writer.WriteElementString("SchemaName", pAttr.SchemaNameToken.AsText);
                }
                
                if (pAttr.ObjectNameToken != null)
                {
                    writer.WriteStartElement("ObjectName");
                    writer.WriteAttributeString("Type", pAttr.ObjectNameToken.DBObjType.ToString());
                    writer.WriteAttributeString("Text", pAttr.ObjectNameToken.AsText);
                    writer.WriteEndElement();
                }

                //if (pAttr.FunctionNameToken != null)
                //{
                //    writer.WriteStartElement("ObjectName");
                //    writer.WriteAttributeString("Type", pAttr.FunctionNameToken.DBObjType.ToString());
                //    writer.WriteAttributeString("Text", pAttr.FunctionNameToken.AsText);
                //    writer.WriteEndElement();
                //}

                if (pAttr.ColumnNameToken != null)
                {
                    writer.WriteStartElement("ColumnName");
                    writer.WriteAttributeString("Text", pAttr.ColumnNameToken.AsText);
                    if (pAttr.ObjectPropertyNameToken != null)
                    {
                        writer.WriteStartElement("PropertyName");
                        writer.WriteAttributeString("Text", pAttr.ObjectPropertyNameToken.AsText);
                        if (pAttr.ObjectProperty2NameToken != null)
                        {
                            writer.WriteStartElement("Property2Name");
                            writer.WriteAttributeString("Text", pAttr.ObjectProperty2NameToken.AsText);
                            writer.WriteEndElement();
                        }
                        if (pAttr.ObjectMethod2NameToken != null)
                        {
                            writer.WriteStartElement("Method2Name");
                            writer.WriteAttributeString("Text", pAttr.ObjectMethod2NameToken.AsText);
                            writer.WriteEndElement();
                        }
                        writer.WriteEndElement();
                    }
                    if (pAttr.ObjectMethodNameToken != null)
                    {
                        writer.WriteStartElement("MethodName");
                        writer.WriteAttributeString("Text", pAttr.ObjectMethodNameToken.AsText);
                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();
                }

                writer.WriteEndElement();
                writer.Flush();
            }

            return XmlText.ToString();
        }

        public static string TLzFieldXml(TLzField pField)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            settings.Indent = true;
            settings.IndentChars = ("    ");

            StringBuilder XmlText = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(XmlText, settings))
            {
                // Write XML data.
                writer.WriteStartElement(pField.GetType().Name);
                writer.WriteAttributeString("type", pField.FieldType.ToString());
                writer.WriteCData(pField.AsText);

                switch (pField.FieldType)
                {
                    case TLzFieldType.lftAttr:
                        writer.WriteRaw(Environment.NewLine+TLz_AttrXml(pField.FieldAttr));
                        break;
                    case TLzFieldType.lftExpression:
                        writer.WriteRaw(Environment.NewLine + TLzCustomExpressionXml(pField.FieldExpr));
                        break;
                    case TLzFieldType.lftSubquery:
                        writer.WriteRaw(Environment.NewLine + commonstmt.TSelectSqlStatementXml((TSelectSqlStatement)pField.SubQuery));
                        break;
                    case TLzFieldType.lftColumn:
                        writer.WriteStartElement("column");
                        writer.WriteAttributeString("name", pField.FieldName);
                        writer.WriteAttributeString("type", (pField.FieldDataType != null) ? pField.FieldDataType.AsText : "none");
                        writer.WriteEndElement();

                        if (pField.ColumnConstraints.Count() > 0)
                        {
                            writer.WriteRaw(TLzConstraintListXml(pField.ColumnConstraints));
                        }

                        break;
                }

                if (pField.FieldAlias.Length > 0)
                {
                    writer.WriteStartElement("fieldlaias");
                    writer.WriteAttributeString("name", pField.FieldAlias);
                    writer.WriteEndElement();
                }
                else if (pField.MssqlOldSyntaxAliasToken != null)
                {
                    writer.WriteStartElement("fieldlaias");
                    writer.WriteAttributeString("name", pField.MssqlOldSyntaxAliasToken.AsText);
                    writer.WriteEndElement();
                }

                writer.WriteEndElement();
                writer.Flush();
            }

            return XmlText.ToString();
        }

        public static string TLzCustomExpressionXml(TLzCustomExpression pExpr)
        {

            pExpr.PostOrderTraverse(exprvisitor);
            //Console.WriteLine("in expr {0} {1}", pExpr.AsText, exprstack.Count);
            return exprstack.Pop().ToString();
        }

        public static Boolean exprvisitor(TLz_Node pnode, Boolean pIsLeafNode)
        {
            TLzCustomExpression lcexpr = (TLzCustomExpression)pnode;

            if (pIsLeafNode)
            {
                //exprwriter.WriteElementString(lcexpr.GetType().Name,lcexpr.AsText);

                XmlWriterSettings settings = new XmlWriterSettings();
                settings.OmitXmlDeclaration = true;
                settings.Indent = true;
                settings.IndentChars = ("    ");

                StringBuilder XmlText = new StringBuilder();
                using (XmlWriter writer = XmlWriter.Create(XmlText, settings))
                {
                    // Write XML data.
                    writer.WriteStartElement(lcexpr.GetType().Name);
                    writer.WriteAttributeString("isleaf", "true");
                    writer.WriteAttributeString("Optype", lcexpr.oper.ToString());

                    switch (lcexpr.oper)
                    {
                        case TLzOpType.Expr_Const:
                            writer.WriteRaw(TLz_ConstXml((TLz_Const)(lcexpr.lexpr)));
                            break;
                        case TLzOpType.Expr_Attr:
                            writer.WriteRaw(TLz_AttrXml((TLz_Attr)(lcexpr.lexpr)));
                            break;
                        case TLzOpType.Expr_FuncCall:
                            writer.WriteRaw(TLz_FuncCallXml((TLz_FuncCall)(lcexpr.lexpr)));
                            break;
                        case TLzOpType.Expr_Case://case expression
                            writer.WriteRaw(TLzCaseExpressionXml((TLzCaseExpression)(lcexpr)));
                            break;
                        case TLzOpType.Expr_subquery:
                            writer.WriteRaw(commonstmt.TSelectSqlStatementXml((TSelectSqlStatement)(lcexpr.lexpr)));
                            break;
                        case TLzOpType.Expr_Exists:
                            writer.WriteRaw(commonstmt.TSelectSqlStatementXml((TSelectSqlStatement)(lcexpr.rexpr)));
                            break;
                        default:
                            writer.WriteRaw(lcexpr.AsText);
                            break;
                    }

                    writer.WriteEndElement();
                    writer.Flush();
                }
                exprstack.Push(XmlText.ToString());
            //Console.WriteLine("in leaf {0} {1}", lcexpr.oper.ToString(),exprstack.Count);
            }
            else
            {

                //Console.WriteLine("in non leaf");
                XmlWriterSettings lcsettings = new XmlWriterSettings();
                lcsettings.OmitXmlDeclaration = true;
                lcsettings.Indent = true;
                lcsettings.IndentChars = ("    ");

                StringBuilder lcXmlText = new StringBuilder();
                using (XmlWriter lcwriter = XmlWriter.Create(lcXmlText, lcsettings))
                {
                    string rexprstr = "";
                    string lexprstr = "";

                    // Write XML data.
                    lcwriter.WriteStartElement(lcexpr.GetType().Name);
                    lcwriter.WriteAttributeString("isleaf", "false");
                    lcwriter.WriteAttributeString("Optype", lcexpr.oper.ToString());
                    if (lcexpr.opname != null)
                    {
                        lcwriter.WriteAttributeString("Opcharacter", lcexpr.opname.AsText);
                    }

                    if (lcexpr.rexpr != null)
                    {
                        rexprstr = exprstack.Pop().ToString();
                    }

                    if (lcexpr.lexpr != null)
                    {
                        lexprstr = exprstack.Pop().ToString();
                    }



                    if (lcexpr.lexpr != null)
                    {
                        lcwriter.WriteRaw(lexprstr);
                    }

                    if (lcexpr.rexpr != null)
                    {
                        lcwriter.WriteRaw(rexprstr);
                    }


                    lcwriter.WriteEndElement();
                    lcwriter.Flush();
                }

                exprstack.Push(lcXmlText.ToString());
            }

            return true;
        }

        public static string TLz_ConstXml(TLz_Const pConst)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            settings.Indent = true;
            settings.IndentChars = ("    ");

            StringBuilder XmlText = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(XmlText, settings))
            {
                // Write XML data.
                writer.WriteStartElement(pConst.GetType().Name);
                writer.WriteAttributeString("valtype", pConst.valtype.ToString());
                writer.WriteString(pConst.RawTokens[0].AsText);
                writer.WriteEndElement();
                writer.Flush();
            }
            return XmlText.ToString();
        }

        public static string TLz_FuncCallXml(TLz_FuncCall pFunc)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            settings.Indent = true;
            settings.IndentChars = ("    ");

            StringBuilder XmlText = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(XmlText, settings))
            {
                // Write XML data.
                writer.WriteStartElement(pFunc.GetType().Name);
                writer.WriteStartElement("FuntionName");
                writer.WriteRaw(TLz_AttrXml(pFunc.funcname));
                writer.WriteEndElement();

                writer.WriteStartElement("Parameters");
                if (pFunc.args != null)
                {
                    for (int i = 0; i < pFunc.args.Count(); i++)
                    {
                        //System.Console.WriteLine(pFunc.args[i].ToString());
                        writer.WriteStartElement("Parameter");
                        // writer.WriteAttributeString("paramtype", pFunc.args[i].GetType().ToString());
                        if (pFunc.args[i] is TSourceToken)
                        {
                            TSourceToken st = (TSourceToken)pFunc.args[i];
                            writer.WriteString(st.AsText);
                        }
                        else if (pFunc.args[i] is TLzCustomExpression)
                        {
                            TLzCustomExpression expr = (TLzCustomExpression)pFunc.args[i];
                            writer.WriteRaw(TLzCustomExpressionXml(expr));
                        }
                        else if (pFunc.args[i] is TLz_TypeName)
                        {
                            TLz_TypeName t = (TLz_TypeName)pFunc.args[i];
                            writer.WriteRaw(TLz_TypeNameXml(t));
                            //writer.WriteCData(t.AsText);
                        }
                        writer.WriteEndElement();
                    }
                }

                if (pFunc.FuncType == TLzFuncType.fntConvert)
                {
                    writer.WriteStartElement("Parameter");
                    writer.WriteRaw(TLz_TypeNameXml(pFunc._ndTypename));
                    writer.WriteEndElement();

                    writer.WriteStartElement("Parameter");
                    writer.WriteRaw(TLzCustomExpressionXml(pFunc._ndExpr as TLzCustomExpression));
                    writer.WriteEndElement();

                    if (pFunc._ndStyle != null)
                    {
                        writer.WriteStartElement("Parameter");
                        writer.WriteRaw(TLzCustomExpressionXml(pFunc._ndStyle as TLzCustomExpression));
                        writer.WriteEndElement();
                    }

                }

                writer.WriteEndElement();

                writer.WriteEndElement();
                writer.Flush();
            }
            return XmlText.ToString();
        }

        public static string TLz_TypeNameXml(TLz_TypeName pType)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            settings.Indent = true;
            settings.IndentChars = ("    ");

            StringBuilder XmlText = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(XmlText, settings))
            {
                // Write XML data.
                writer.WriteStartElement(pType.GetType().Name);
                writer.WriteAttributeString("type", pType.ft.ToString());
                writer.WriteString(pType.AsText);
                writer.WriteEndElement();
                writer.Flush();
            }
            return XmlText.ToString();
        }

        public static string TLzCaseExpressionXml(TLzCaseExpression pExpr)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            settings.Indent = true;
            settings.IndentChars = ("    ");
            settings.ConformanceLevel = ConformanceLevel.Fragment;


            StringBuilder XmlText = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(XmlText, settings))
            {
                // Write XML data.
                if (pExpr.ConditionExpr != null)
                {
                    writer.WriteStartElement("ConditionExpr");
                    writer.WriteRaw(TLzCustomExpressionXml(pExpr.ConditionExpr));
                    writer.WriteEndElement();
                }

                if (pExpr.WhenThenList.Count() > 0)
                {
                    writer.WriteStartElement("caseexpr_whenclauselist");
                    for (int i = 0; i < pExpr.WhenThenList.Count(); i++)
                    {
                        writer.WriteRaw(TLzCaseWhenXml((TLzCaseWhen)(pExpr.WhenThenList[i])));
                    }
                    writer.WriteEndElement();
                }

                if (pExpr.DefaultExpr != null)
                {
                    writer.WriteStartElement("caseexpr_default");
                    writer.WriteRaw(TLzCustomExpressionXml(pExpr.DefaultExpr));
                    writer.WriteEndElement();
                }

                writer.Flush();
            }
            return XmlText.ToString();
        }

        public static string TLzCaseWhenXml(TLzCaseWhen pCaseWhen)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            settings.Indent = true;
            settings.IndentChars = ("    ");

            StringBuilder XmlText = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(XmlText, settings))
            {
                // Write XML data.
                writer.WriteStartElement(pCaseWhen.GetType().Name);

                writer.WriteStartElement("caseexpr_whenclauseitemcondition");
                writer.WriteRaw(TLzCustomExpressionXml(pCaseWhen.WhenExpr));
                writer.WriteEndElement();

                writer.WriteStartElement("caseexpr_whenclauseitemresult");
                writer.WriteRaw(TLzCustomExpressionXml(pCaseWhen.ThenExpr));
                writer.WriteEndElement();

                writer.WriteEndElement();
                writer.Flush();
            }
            return XmlText.ToString();

        }

        public static string TLzOrderByXml(TLzOrderBy pOrderBy)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            settings.Indent = true;
            settings.IndentChars = ("    ");

            StringBuilder XmlText = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(XmlText, settings))
            {
                // Write XML data.
                writer.WriteStartElement(pOrderBy.GetType().Name);
                writer.WriteAttributeString("SortType", pOrderBy.SortType.ToString());
                if (pOrderBy.SortItemType == TLzSortItemType.sitExpression){
                    writer.WriteRaw(TLzCustomExpressionXml(pOrderBy.SortExpr));
                }
                //writer.WriteString(pConst.RawTokens[0].AsText);
                writer.WriteEndElement();
                writer.Flush();
            }
            return XmlText.ToString();
        }

        public static string TCommonTableExpressionXml(TCommonTableExpression pCte)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            settings.Indent = true;
            settings.IndentChars = ("    ");

            StringBuilder XmlText = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(XmlText, settings))
            {
                // Write XML data.
                writer.WriteStartElement(pCte.GetType().Name);
                
                writer.WriteStartElement("expression_name");
                writer.WriteRaw(TLz_AttrXml(pCte.ndTablename));
                writer.WriteEndElement();

                if (pCte.lstNamelist != null){
                writer.WriteStartElement("columnlist");
                for (int i = 0; i < pCte.lstNamelist.Count(); i++)
                {
                    TSourceToken st = (TSourceToken)(pCte.lstNamelist[i]);
                    writer.WriteElementString("column", st.AsText);
                }
                writer.WriteEndElement();
                }

                writer.WriteRaw(commonstmt.TSelectSqlStatementXml(pCte.ndSelect_with_parens));
                writer.WriteEndElement();
                writer.Flush();
            }
            return XmlText.ToString();
        }


        public static string TLzTopClauseXml(TLzTopClause pTop)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            settings.Indent = true;
            settings.IndentChars = ("    ");

            StringBuilder XmlText = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(XmlText, settings))
            {
                // Write XML data.
                writer.WriteStartElement(pTop.GetType().Name);
                writer.WriteAttributeString("percent", (pTop._stPercent == null) ? "false":"true" );
                writer.WriteAttributeString("withties", (pTop._stTies == null) ? "false" : "true");
                if (pTop._ndExpr != null)
                {
                    writer.WriteRaw(TLzCustomExpressionXml((TLzCustomExpression)(pTop._ndExpr)));
                }else if (pTop._ndQuery != null){
                    writer.WriteRaw(commonstmt.TSelectSqlStatementXml((TSelectSqlStatement)(pTop._ndQuery)));
                }
                writer.WriteEndElement();
                writer.Flush();
            }
            return XmlText.ToString();
        }

        public static string TLzJoinListXml(TLzJoinList pJoinList)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            settings.Indent = true;
            settings.IndentChars = ("    ");

            StringBuilder XmlText = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(XmlText, settings))
            {
                // Write XML data.
                writer.WriteStartElement(pJoinList.GetType().Name);
                foreach (TLzJoin join in pJoinList)
                {
                    writer.WriteRaw(basicnodexml.TLzJoinXml(join));
                }
                writer.WriteEndElement();
                writer.Flush();
            }
            return XmlText.ToString();
        }

        public static string TLzJoinXml(TLzJoin pJoin)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            settings.Indent = true;
            settings.IndentChars = ("    ");

            StringBuilder XmlText = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(XmlText, settings))
            {
                // Write XML data.
                writer.WriteStartElement(pJoin.GetType().Name);
                writer.WriteAttributeString("JoinTableType", pJoin.JoinTableType.ToString());
                if (pJoin.JoinTable != null)
                {
                    writer.WriteRaw(TLzTableXml(pJoin.JoinTable));
                }
                if (pJoin.JoinJoin != null)
                {
                    writer.WriteRaw(TLzJoinXml(pJoin.JoinJoin));
                }

                writer.WriteRaw(TLzJoinItemListXml(pJoin.JoinItems));

                if (pJoin.newalias != null)
                {
                    writer.WriteRaw(TLz_AliasClauseXml(pJoin.newalias));
                }

                writer.WriteEndElement();
                writer.Flush();
            }
            return XmlText.ToString();
        }

        public static string TLzJoinItemListXml(TLzJoinItemList pJoinItemList)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            settings.Indent = true;
            settings.IndentChars = ("    ");

            StringBuilder XmlText = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(XmlText, settings))
            {
                // Write XML data.
                writer.WriteStartElement(pJoinItemList.GetType().Name);
                foreach (TLzJoinItem joinitem in pJoinItemList)
                {
                    writer.WriteRaw(basicnodexml.TLzJoinItemXml(joinitem));
                }
                writer.WriteEndElement();
                writer.Flush();
            }
            return XmlText.ToString();
        }

        public static string TLzJoinItemXml(TLzJoinItem pJoinItem)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            settings.Indent = true;
            settings.IndentChars = ("    ");

            StringBuilder XmlText = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(XmlText, settings))
            {
                // Write XML data.
                writer.WriteStartElement(pJoinItem.GetType().Name);
                writer.WriteAttributeString("JoinItemTableType", pJoinItem.JoinItemTableType.ToString());
                writer.WriteAttributeString("JoinType", pJoinItem.JoinType.ToString());
                if (pJoinItem.JoinItemTable != null)
                {
                    writer.WriteRaw(TLzTableXml(pJoinItem.JoinItemTable));
                }

                if (pJoinItem.JoinItemJoin != null)
                {
                    writer.WriteRaw(TLzJoinXml(pJoinItem.JoinItemJoin));
                }

                if (pJoinItem.JoinQual != null)
                {
                    writer.WriteStartElement("JoinQual");
                    writer.WriteAttributeString("JoinQualType", pJoinItem.JoinQualType.ToString());
                    writer.WriteRaw(TLzCustomExpressionXml(pJoinItem.JoinQual));
                    writer.WriteEndElement();
                }
                
                writer.WriteEndElement();
                writer.Flush();
            }
            return XmlText.ToString();
        }

        public static string TLzTableXml(TLzTable pTable)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            settings.Indent = true;
            settings.IndentChars = ("    ");

            StringBuilder XmlText = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(XmlText, settings))
            {
                // Write XML data.
                writer.WriteStartElement(pTable.GetType().Name);
                writer.WriteAttributeString("TableType", pTable.TableType.ToString());
                switch (pTable.TableType)
                {
                    case TLzTableType.lttAttr:
                        writer.WriteRaw(TLz_AttrXml(pTable.TableAttr));
                        break;
                    case TLzTableType.lttfunc:
                        writer.WriteRaw(TLz_FuncCallXml(pTable.TableFunc));
                        break;
                    case TLzTableType.lttSubquery:
                        writer.WriteRaw(commonstmt.TSelectSqlStatementXml((TSelectSqlStatement)pTable.SubQuery));
                        break;
                    default:
                        break;
                }

                if (pTable.AliasClause != null)
                {
                    writer.WriteRaw(TLz_AliasClauseXml(pTable.AliasClause));
                }

                writer.WriteEndElement();
                writer.Flush();
            }
            return XmlText.ToString();
        }

        public static string TLz_AliasClauseXml(TLz_AliasClause pAlias)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            settings.Indent = true;
            settings.IndentChars = ("    ");

            StringBuilder XmlText = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(XmlText, settings))
            {
                // Write XML data.
                writer.WriteStartElement(pAlias.GetType().Name);
                writer.WriteAttributeString("withas",(pAlias._withas == null) ? "false" : "true");
                writer.WriteString(pAlias.AliasToken.AsText);
                writer.WriteEndElement();
                writer.Flush();
            }
            return XmlText.ToString();
        }

        public static string TLzGroupByItemXml(TLzGroupByItem pGroupByItem)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            settings.Indent = true;
            settings.IndentChars = ("    ");

            StringBuilder XmlText = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(XmlText, settings))
            {
                // Write XML data.
                writer.WriteStartElement(pGroupByItem.GetType().Name);
                //writer.WriteRaw(pGroupByItem.AsText);
                writer.WriteRaw(TLzCustomExpressionXml((TLzCustomExpression)(pGroupByItem._ndExpr)));

                writer.WriteEndElement();
                writer.Flush();
            }
            return XmlText.ToString();
        }

        public static string TLzGroupByXml(TLzGroupBy pGroupBy)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            settings.Indent = true;
            settings.IndentChars = ("    ");

            StringBuilder XmlText = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(XmlText, settings))
            {
                // Write XML data.
                writer.WriteStartElement(pGroupBy.GetType().Name);
                for (int i = 0; i < pGroupBy.GroupItems.Count(); i++)
                {
                   // Console.WriteLine(i);
                    TLzGroupByItem lcitem = pGroupBy.GroupItems[i] as TLzGroupByItem;
                    writer.WriteRaw(TLzGroupByItemXml(lcitem));
                }
                writer.WriteEndElement();
                writer.Flush();
            }
            return XmlText.ToString();
        }

        public static string TLzConstraintColumnXml(TLzConstraintColumn pConstraintColumn)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            settings.Indent = true;
            settings.IndentChars = ("    ");

            StringBuilder XmlText = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(XmlText, settings))
            {
                // Write XML data.
                writer.WriteStartElement(pConstraintColumn.GetType().Name);
                writer.WriteRaw(TLz_AttrXml(pConstraintColumn.ColumnName));
                writer.WriteEndElement();
                writer.Flush();
            }
            return XmlText.ToString();
        }

        public static string TLzConstraintRefClauseXml(TLzConstraintRefClause pConstraintRefClause)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            settings.Indent = true;
            settings.IndentChars = ("    ");

            StringBuilder XmlText = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(XmlText, settings))
            {
                // Write XML data.
                writer.WriteStartElement(pConstraintRefClause.GetType().Name);

                writer.WriteStartElement("reftable");
                writer.WriteRaw(TLz_AttrXml(pConstraintRefClause.q_name));
                writer.WriteEndElement();

                if (pConstraintRefClause.ConstraintColumnList.Count() > 0)
                {
                    writer.WriteRaw(TLzConstraintColumnListXml(pConstraintRefClause.ConstraintColumnList));
                }
                
                writer.WriteEndElement();
                writer.Flush();
            }
            return XmlText.ToString();
        }

        public static string TLzConstraintColumnListXml(TLzConstraintColumnList pConstraintColumnList)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            settings.Indent = true;
            settings.IndentChars = ("    ");

            StringBuilder XmlText = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(XmlText, settings))
            {
                // Write XML data.
                writer.WriteStartElement(pConstraintColumnList.GetType().Name);
                foreach (TLzConstraintColumn ConstraintColumn in pConstraintColumnList)
                {
                    writer.WriteRaw(basicnodexml.TLzConstraintColumnXml(ConstraintColumn));
                }
                writer.WriteEndElement();
                writer.Flush();
            }
            return XmlText.ToString();
        }

        public static string TLzFieldListXml(TLzFieldList pFieldList)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            settings.Indent = true;
            settings.IndentChars = ("    ");

            StringBuilder XmlText = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(XmlText, settings))
            {
                // Write XML data.
                writer.WriteStartElement(pFieldList.GetType().Name);
                foreach (TLzField field in pFieldList)
                {
                    writer.WriteRaw(basicnodexml.TLzFieldXml(field));
                }
                writer.WriteEndElement();
                writer.Flush();
            }
            return XmlText.ToString();
        }

        public static string TLzConstraintListXml(TLzConstraintList pConstraintList)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            settings.Indent = true;
            settings.IndentChars = ("    ");

            StringBuilder XmlText = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(XmlText, settings))
            {
                // Write XML data.
                writer.WriteStartElement(pConstraintList.GetType().Name);
                foreach (TLzConstraint Constraint in pConstraintList)
                {
                    writer.WriteRaw(basicnodexml.TLzConstraintXml(Constraint));
                }
                writer.WriteEndElement();
                writer.Flush();
            }
            return XmlText.ToString();
        }

        public static string TLzConstraintXml(TLzConstraint pConstraint)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;
            settings.Indent = true;
            settings.IndentChars = ("    ");

            StringBuilder XmlText = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(XmlText, settings))
            {
                // Write XML data.
                writer.WriteStartElement(pConstraint.GetType().Name);
                writer.WriteAttributeString("ConstraintType", pConstraint.ConstraintType.ToString());
                writer.WriteAttributeString("ConstraintLevel", pConstraint.ConstraintLevel.ToString());
                switch (pConstraint.ConstraintType)
                {
                    case TLzConstraintType.ctPrimarykey:
                        if (pConstraint.ConstraintLevel == TLzConstraintLevel.clTable)
                        {
                            writer.WriteRaw(pConstraint.ColumnNameList.AsText);
                        }
                        break;
                    case TLzConstraintType.ctUnique:
                        if (pConstraint.ConstraintLevel == TLzConstraintLevel.clTable)
                        {
                            writer.WriteRaw(pConstraint.ColumnNameList.AsText);
                        }
                        break;
                    case TLzConstraintType.ctForeignkey:
                        if (pConstraint.ConstraintLevel == TLzConstraintLevel.clTable)
                        {
                            writer.WriteRaw(pConstraint.ColumnNameList.AsText);
                        }
                        break;
                    case TLzConstraintType.ctReference:
                        writer.WriteRaw(TLzConstraintRefClauseXml(pConstraint.RefClause));
                        break;
                    case TLzConstraintType.ctCheck:
                        writer.WriteRaw(basicnodexml.TLzCustomExpressionXml((TLzCustomExpression)pConstraint.ColExpr));
                        break;
                    case TLzConstraintType.ctDefault:
                        writer.WriteRaw(basicnodexml.TLzCustomExpressionXml((TLzCustomExpression)pConstraint.ColExpr));
                        break;
                    default:
                        break;
                }
                writer.WriteEndElement();
                writer.Flush();
            }
            return XmlText.ToString();
        }
    }
}
